package com.ljw.test.dao;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.ljw.test.model.Child;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "CHILD".
*/
public class ChildDao extends AbstractDao<Child, Long> {

    public static final String TABLENAME = "CHILD";

    /**
     * Properties of entity Child.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property ParentId = new Property(1, Long.class, "parentId", false, "PARENT_ID");
        public final static Property Child = new Property(2, String.class, "child", false, "CHILD");
        public final static Property Time = new Property(3, int.class, "time", false, "TIME");
    }

    private Query<Child> parent_ChildListQuery;

    public ChildDao(DaoConfig config) {
        super(config);
    }
    
    public ChildDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"CHILD\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"PARENT_ID\" INTEGER NOT NULL ," + // 1: parentId
                "\"CHILD\" TEXT NOT NULL ," + // 2: child
                "\"TIME\" INTEGER NOT NULL );"); // 3: time
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"CHILD\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, Child entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getParentId());
        stmt.bindString(3, entity.getChild());
        stmt.bindLong(4, entity.getTime());
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, Child entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getParentId());
        stmt.bindString(3, entity.getChild());
        stmt.bindLong(4, entity.getTime());
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public Child readEntity(Cursor cursor, int offset) {
        Child entity = new Child( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.getLong(offset + 1), // parentId
            cursor.getString(offset + 2), // child
            cursor.getInt(offset + 3) // time
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, Child entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setParentId(cursor.getLong(offset + 1));
        entity.setChild(cursor.getString(offset + 2));
        entity.setTime(cursor.getInt(offset + 3));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(Child entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(Child entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(Child entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "childList" to-many relationship of Parent. */
    public List<Child> _queryParent_ChildList(Long parentId) {
        synchronized (this) {
            if (parent_ChildListQuery == null) {
                QueryBuilder<Child> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ParentId.eq(null));
                parent_ChildListQuery = queryBuilder.build();
            }
        }
        Query<Child> query = parent_ChildListQuery.forCurrentThread();
        query.setParameter(0, parentId);
        return query.list();
    }

}
